﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Entitys.Sys;
using Core.Toolkit.DbInitializer;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.SimpleTemp.Repositories.CBS
{
    /// <summary>
    /// 隐患数据初始化
    /// </summary>
    public class YH_DBinit : DbInitializer
    {

        public SysFactory GetRandomFactory(DbContext context)
        {
            var list = context.Set<SysFactory>().Where(a => a.FactoryType != "1").ToList();
            return list[new Random().Next(0, list.Count() - 1)];
        }

        public YH_DBinit()
        {
            Sort = 1;
        }
        public override void Initialize(DbContext context, WebAppOptions _webAppOptions)
        {
            var adminUser = context.Set<SysUser>().Where(a => a.LoginName == _webAppOptions.AdminLoginName).FirstOrDefault();
            var sysAreaList = context.Set<SysArea>().ToList();
            var sysDepartmentList = context.Set<SysDepartment>().ToList();
            #region 基础_隐患等级
            List<YH_Grade> YH_GradeList = new List<YH_Grade>();
            YH_GradeList.Add(new YH_Grade()
            {
                Level = "一级",
                Points = 1
            });
            YH_GradeList.Add(new YH_Grade()
            {
                Level = "二级",
                Points = 1
            });
            YH_GradeList.Add(new YH_Grade()
            {
                Level = "三级",
                Points = 1
            });
            YH_GradeList.Add(new YH_Grade()
            {
                Level = "四级",
                Points = 1
            });
            YH_GradeList.Add(new YH_Grade()
            {
                Level = "重大",
                Points = 1
            });
            context.Set<YH_Grade>().AddRange(YH_GradeList);
            #endregion


            #region 隐患_隐患排查类别
            List<YH_Category> YH_CategoryList = new List<YH_Category>();

            YH_CategoryList.Add(new YH_Category()
            {
                Id = Guid.NewGuid(),
                Category = "类别A",
            });
            YH_CategoryList.Add(new YH_Category()
            {
                Id = Guid.NewGuid(),
                Category = "类别B",
            });
            YH_CategoryList.Add(new YH_Category()
            {
                Id = Guid.NewGuid(),
                Category = "类别C",
            });

            context.Set<YH_Category>().AddRange(YH_CategoryList);
            #endregion


            #region 隐患_基础管理类隐患类型
            List<YH_BasisType> YH_BasisTypeList = new List<YH_BasisType>();

            YH_BasisTypeList.Add(new YH_BasisType()
            {
                Id = Guid.NewGuid(),
                Name = "类型A",
            });
            YH_BasisTypeList.Add(new YH_BasisType()
            {
                Id = Guid.NewGuid(),
                Name = "类型B",
            });
            YH_BasisTypeList.Add(new YH_BasisType()
            {
                Id = Guid.NewGuid(),
                Name = "类型C",
            });

            context.Set<YH_BasisType>().AddRange(YH_BasisTypeList);
            #endregion



            //隐患_基础管理类排查库YH_BasisInformation
            List<YH_BasisInformation> YH_BasisInformationList = new List<YH_BasisInformation>();
            for (int i = 0; i < 10; i++)
            {
                YH_BasisInformationList.Add(new YH_BasisInformation()
                {
                    TypeID = YH_BasisTypeList[new Random().Next(0, 2)].Id,
                    RiskLevel = YH_GradeList[new Random().Next(0, 3)].Id,
                    InvestigationProject = "排查项目 =" + i,
                    InvestigationContents = "排查内容与排查标准 =" + i
                });
            }
            context.Set<YH_BasisInformation>().AddRange(YH_BasisInformationList);


            ///隐患_隐患台账
            List<YH_StandingBook> YH_StandingBookList = new List<YH_StandingBook>();
            for (int i = 0; i < 100; i++)
            {


                YH_StandingBookList.Add(new YH_StandingBook()
                {
                    Discoverer = adminUser.Id,
                    RiskRegion = sysAreaList[new Random().Next(0, sysAreaList.Count() - 1)].Id,//隐患所属区域
                    RiskPosition = Guid.NewGuid(),//new Random().Next(0, 1).ToString(),//隐患位置
                    OrganizationType = new Random().Next(103001, 103002).ToString(),//组织类别
                    InspectType = new Random().Next(0, 3).ToString(),//检查类别//YH_Category
                    DiscoveryTime = DbInitializerHelp.GetRandomDate(),//隐患发现时间
                    RiskType = Guid.NewGuid(),//隐患分类
                    RiskLevel = YH_GradeList[new Random().Next(0, 2)].Id,//隐患等级ID
                    RiskDescription = "隐患描述" + i,//隐患描述
                    RectificationDescription = "整改描述" + i,//整改描述
                    Schedule = new Random().Next(0, 4).ToString(),
                    Status = new Random().Next(0, 1).ToString(),
                    Type = new Random().Next(0, 2).ToString(),
                    YH_StandingBookAttachment = new List<YH_StandingBookAttachment>()
                    {
                        new YH_StandingBookAttachment(){
                             Attachment=Guid.NewGuid(),
                             Type="0"
                        },
                         new YH_StandingBookAttachment(){
                             Attachment=Guid.NewGuid(),
                             Type="1"
                        },
                          new YH_StandingBookAttachment(){
                             Attachment=Guid.NewGuid(),
                             Type="1"
                        }

                    }
                });
            }
            context.Set<YH_StandingBook>().AddRange(YH_StandingBookList);


            //隐患_排查计划主表（隐患排查任务）YH_ScStandardTypereeningPlan
            List<YH_ScreeningPlan> YH_ScreeningPlanList = new List<YH_ScreeningPlan>();
            for (int i = 0; i < 100; i++)
            {
                //隐患_基础管理类排查库
                var basisInformation = YH_BasisInformationList[new Random().Next(0, 9)];
                YH_ScreeningPlanList.Add(new YH_ScreeningPlan()
                {
                    Type = new Random().Next(0, 2).ToString(),
                    Objective = "排查目的" + i,//排查目的
                    PersonInCharge = adminUser.Id,//负责人Id
                    BeginTime = DbInitializerHelp.GetRandomDate(),//计划开始时间
                    Unit = sysDepartmentList[new Random().Next(0, sysDepartmentList.Count() - 1)].Id,
                    Requirements = "排查要求" + i,
                    Content = "排查内容" + i,
                    InvestigationPost = "排查岗位" + i,
                    StandardType = new Random().Next(0, 2).ToString(),//排查标准类型
                    //排查项
                    YH_ScreeningPlanProject = new List<YH_ScreeningPlanProject>() {
                        new YH_ScreeningPlanProject(){
                            Project="排查项目"+i,
                            Remark="备注说明"+i,

                            Region=sysAreaList[new Random().Next(0, sysAreaList.Count() - 1)].Id,//区域
                            //隐患_排查计划项目明细
                            YH_ScreeningPlanProDetail=new List<YH_ScreeningPlanProDetail>(){
                                new YH_ScreeningPlanProDetail(){
                                    HazardTypeID=basisInformation.TypeID,
                                    StandardID=basisInformation.Id,
                                    StandardContent=basisInformation.InvestigationContents,
                                    Type=new Random().Next(0, 1).ToString(),
                                    RiskLevel=basisInformation.RiskLevel
                                },
                                new YH_ScreeningPlanProDetail(){
                                    HazardTypeID=basisInformation.TypeID,
                                    StandardID=basisInformation.Id,
                                    StandardContent=basisInformation.InvestigationContents,
                                    Type=new Random().Next(0, 1).ToString(),
                                    RiskLevel=basisInformation.RiskLevel
                                }
                            },

                        }
                    },
                    YH_ScreeningPlanUser = new List<YH_ScreeningPlanUser>() {
                        new YH_ScreeningPlanUser(){
                             UserId=adminUser.Id
                        }
                        ,new YH_ScreeningPlanUser(){
                             UserId=adminUser.Id
                        }
                    }
                }); ;
            }
            context.Set<YH_ScreeningPlan>().AddRange(YH_ScreeningPlanList);
            List<JG_STOPObserveMain> JG_STOPObserveMainlist = new List<JG_STOPObserveMain>();
            for (int i = 0; i < 10; i++)
            {
                JG_STOPObserveMainlist.Add(new JG_STOPObserveMain()
                {
                    evaluationc = new Random().Next(0, 1) == 1,//所有皆安全
                    observePeople = adminUser.Id,//观察人
                    department = sysDepartmentList[new Random().Next(0, sysDepartmentList.Count() - 1)].Id,//部门
                    shifts = new Random().Next(0, 2).ToString(),//班次
                    situation = sysAreaList[new Random().Next(0, sysAreaList.Count() - 1)].Id,//区域
                    observaDate = DbInitializerHelp.GetRandomDate(),//观察日期
                    ryfy_remark = "人员反映备注" + i,//人员反映备注
                    grfhzb_remark = "个人防护装备备注" + i,//个人防护装备备注
                    rydwz_remark = "人员的位子备注" + i,//人员的位子备注
                    gjjsb_remark = "工具及设备备注" + i,//工具及设备备注
                    cx_remark = "程序备注" + i,//程序备注
                    zxbz_remark = "秩序标准秩序标准" + i,//秩序标准秩序标准
                    encourage = "安全行为" + i,//安全行为
                    correct = "不安全行为" + i,//不安全行为
                });
            }

            ///随机详情数据
            JG_STOPObserveMainlist.ForEach(item =>
            {
                item.JG_STOPObserveDetail = new List<JG_STOPObserveDetail>();
                for (int i = 9040; i <= 9045; i++)
                {
                    item.JG_STOPObserveDetail.Add(new JG_STOPObserveDetail()
                    {
                        BigType = i.ToString(),
                        SmallType = new Random().Next(0, 2).ToString()
                    });

                    item.JG_STOPObserveDetail.Add(new JG_STOPObserveDetail()
                    {
                        BigType = i.ToString(),
                        SmallType = new Random().Next(0, 2).ToString()
                    });
                }
            });

            context.Set<JG_STOPObserveMain>().AddRange(JG_STOPObserveMainlist);


            //隐患_操作与相关作业活动YH_OperationAndWork
            List<YH_OperationAndWork> YH_OperationAndWorkList = new List<YH_OperationAndWork>();
            for (int i = 0; i < 10; i++)
            {
                var factoryId = GetRandomFactory(context).Id;
                var areaList = sysAreaList.Where(a => a.SubFactory == factoryId).ToList();
                YH_OperationAndWorkList.Add(new YH_OperationAndWork()
                {
                    AreaID = areaList[new Random().Next(0, areaList.Count() - 1)].Id,//区域
                    JobSteps = "作业步骤" + i,
                    Task = "作业任务" + i,
                    RiskLevel = YH_GradeList[new Random().Next(0, 2)].Id,//隐患等级ID
                    CheckStandard = "检查标准" + i,
                    RiskOfViolation = "违反标准的风险" + i,
                    CreateFactory = factoryId

                });
            }



            context.Set<YH_OperationAndWork>().AddRange(YH_OperationAndWorkList);

            //隐患_检维修作业活动
            List<YH_CheckRepair> YH_CheckRepairList = new List<YH_CheckRepair>();
            for (int i = 0; i < 10; i++)
            {
                var factoryId = GetRandomFactory(context).Id;
                var areaList = sysAreaList.Where(a => a.SubFactory == factoryId).ToList();

                YH_CheckRepairList.Add(new YH_CheckRepair()
                {


                    AreaID = areaList[new Random().Next(0, areaList.Count() - 1)].Id,//区域
                    JobModule = "作业模块" + i,
                    RepairItems = "维修项目" + i,
                    JobActivities = "维修活动" + i,
                    RiskLevel = YH_GradeList[new Random().Next(0, 2)].Id,//隐患等级ID
                    CheckStandard = "检查标准" + i,
                    RiskOfViolation = "违反标准的风险" + i,
                    CreateFactory = factoryId

                }); ;
            }

            context.Set<YH_CheckRepair>().AddRange(YH_CheckRepairList);



            //隐患_设备设施
            List<YH_Equipment> YH_EquipmentList = new List<YH_Equipment>();
            for (int i = 0; i < 10; i++)
            {
                var factoryId = GetRandomFactory(context).Id;
                var areaList = sysAreaList.Where(a => a.SubFactory == factoryId).ToList();
                YH_EquipmentList.Add(new YH_Equipment()
                {
                    AreaID = areaList[new Random().Next(0, areaList.Count() - 1)].Id,//区域

                    Equipment = "设备" + i,
                    FunctionalPart = "功能部分" + i,
                    SubfunctionalPart = "子功能部分" + i,
                    Parts = "部件:" + i,
                    ExpectedFunction = "预期功能" + i,
                    Fault = "故障" + i,
                    CheckStandard = "检查标准" + i,
                    RiskOfViolation = "违反标准的风险" + i,
                    RiskLevel = YH_GradeList[new Random().Next(0, 2)].Id,//隐患等级ID
                    CreateFactory = factoryId

                });
            }

            context.Set<YH_Equipment>().AddRange(YH_EquipmentList);



            //隐患_设备设施
            List<YH_Process> YH_ProcessList = new List<YH_Process>();
            for (int i = 0; i < 10; i++)
            {
                var factoryId = GetRandomFactory(context).Id;
                var areaList = sysAreaList.Where(a => a.SubFactory == factoryId).ToList();
                YH_ProcessList.Add(new YH_Process()
                {
                    AreaID = areaList[new Random().Next(0, areaList.Count() - 1)].Id,//区域
                    Factor = "要素特性" + i,
                    Introducer = "引导词" + i,
                    Deviation = "偏差" + i,
                    CheckStandard = "检查标准" + i,
                    RiskOfViolation = "违反标准的风险" + i,
                    RiskLevel = YH_GradeList[new Random().Next(0, 2)].Id,//隐患等级ID
                    CreateFactory = factoryId

                });
            }

            context.Set<YH_Process>().AddRange(YH_ProcessList);


            //隐患_场所环境
            List<YH_Environment> YH_EnvironmentList = new List<YH_Environment>();
            for (int i = 0; i < 10; i++)
            {
                var factoryId = GetRandomFactory(context).Id;
                var areaList = sysAreaList.Where(a => a.SubFactory == factoryId).ToList();
                YH_EnvironmentList.Add(new YH_Environment()
                {
                    AreaID = areaList[new Random().Next(0, areaList.Count() - 1)].Id,//区域
                    PlaceEnvironment = "场所环境" + i,
                    EnergySource = "能量源" + i,
                    CheckStandard = "检查标准" + i,
                    RiskOfViolation = "违反标准的风险" + i,
                    RiskLevel = YH_GradeList[new Random().Next(0, 2)].Id,//隐患等级ID
                    CreateFactory = factoryId

                });
            }

            context.Set<YH_Environment>().AddRange(YH_EnvironmentList);


            context.SaveChanges();
        }
    }
}

